#!/usr/bin/env python

# scrape_fb_objects.py


import json
import types
import requests
import sys, os, inspect
from random import randint
from time import sleep
import sqlite3

# disable warnings
import requests.packages.urllib3
requests.packages.urllib3.disable_warnings()

# Define setting variables
DB = ''
targets = sys.argv[1] + '.sqlite' # Database containing target objects
keep_paginating = True # Boolean
continue_previous = True 
##

# This find path to additional modules in the same directory
# realpath() with make your script run, even if you symlink it :)
cmd_folder = os.path.realpath(os.path.abspath(os.path.split(inspect.getfile( inspect.currentframe() ))[0]))
if cmd_folder not in sys.path:
    sys.path.insert(0, cmd_folder)

# use this if you want to include modules from a subforder
cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile( inspect.currentframe() ))[0],"subfolder")))
if cmd_subfolder not in sys.path:
    sys.path.insert(0, cmd_subfolder)

from fun import *
from local_info import *

# Loop

conn = sqlite3.connect(targets, timeout=10)
conn.text_factory = str
cursor = conn.cursor()
cursor.execute("SELECT id, since_unix, until_unix FROM target WHERE status = 0")
results = cursor.fetchall()

for result in results:

    try:
        object_id =  result[0]
        since_unix = result[1]
        until_unix = result[2]
        print ('FB Object: ' + object_id + '(since '+ since_unix + ' and until '+ until_unix +')')
        scrapeFbObject(object_id, ACCESS_TOKEN, DB, keep_paginating, continue_previous, mail_spec, parse_comments = True, since = since_unix, until = until_unix)

        conn = sqlite3.connect(targets, timeout=10)
        conn.text_factory = str
        cursor = conn.cursor()
        cursor.execute("UPDATE target SET status = 1 WHERE id = ?", (object_id,))
        conn.commit()
          
        sleep(15)
    except Exception,e:
        print str(e)
        pass

conn.commit()
 
